package com.zmc.product.controller;


import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.product.infrastructure.bean.vo.CategoryPropertyValueVo;
import com.zmc.product.infrastructure.entity.CommodityCategoryPropertyValueEntity;
import com.zmc.product.infrastructure.bean.query.CommodityCategoryPropertyValueQuery;
import com.zmc.product.infrastructure.entity.CommodityGoodsEntity;
import com.zmc.product.infrastructure.service.CommodityCategoryPropertyValueService;
import com.zmc.product.infrastructure.bean.vo.PropertyValueVo;
import com.zmc.product.infrastructure.service.CommodityGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品中心的属性Controller
 *
 * @author zmc
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/value")
public class CategoryPropertyValueController {

    @Autowired
    private CommodityCategoryPropertyValueService commodityCategoryPropertyValueService;

    @Autowired
    private CommodityGoodsService commodityGoodsService;

    @Autowired
    private AuthTokenService authTokenService;

    /**
     * 查询商品中心的属性列表
     */
    @PostMapping("/list")
    public R list(@RequestBody CommodityCategoryPropertyValueQuery query) {
        PageUtils page = commodityCategoryPropertyValueService.pageCommodityCategoryPropertyValueList(query);
        return R.page(page);
    }

    /**
     * 导出商品中心的属性列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityCategoryPropertyValueQuery query) {
        List<CommodityCategoryPropertyValueEntity> list = commodityCategoryPropertyValueService.selectCommodityCategoryPropertyValueList(query);
        ExcelUtil<CommodityCategoryPropertyValueEntity> util = new ExcelUtil<>(CommodityCategoryPropertyValueEntity.class);
        util.exportExcel(response, list, "商品中心的属性数据");
    }

    /**
     * 获取商品中心的属性详细信息
     */
    @PostMapping(value = "/info")
    public R getInfo(@RequestBody CommodityCategoryPropertyValueEntity entity) {
        return R.ok(commodityCategoryPropertyValueService.getById(entity.getId()));
    }

    /**
     * 新增商品中心的属性
     */
    @PostMapping("/add")
    public R add(@RequestBody CommodityCategoryPropertyValueEntity commodityCategoryPropertyValue) {
        String username = authTokenService.getUserInfo().getUsername();
        commodityCategoryPropertyValue.setCreateBy(username);
        commodityCategoryPropertyValueService.save(commodityCategoryPropertyValue);
        return R.ok();
    }

    /**
     * 修改商品中心的属性
     */
    @PostMapping("/update")
    public R edit(@RequestBody CommodityCategoryPropertyValueEntity commodityCategoryPropertyValue) {
        String username = authTokenService.getUserInfo().getUsername();
        commodityCategoryPropertyValue.setUpdateBy(username);
        commodityCategoryPropertyValueService.updateById(commodityCategoryPropertyValue);
        return R.ok();
    }

    /**
     * 删除商品中心的属性
     */
    @PostMapping("/delete")
    public R remove(@RequestBody CommodityCategoryPropertyValueEntity entity) {
        commodityCategoryPropertyValueService.removeById(entity.getId());
        return R.ok();
    }

    @PostMapping("/spu")
    public R getGoodsValue(@RequestBody CategoryPropertyValueVo vo) {
        List<PropertyValueVo> list = commodityCategoryPropertyValueService.getGoodsSpuPropertyValueListByCategoryIdAndType(vo.getCategoryId());
        return R.ok(list);
    }


    @PostMapping("/sku")
    public R getSkuValues(@RequestBody CategoryPropertyValueVo vo) {
        CommodityGoodsEntity goodsEntity = commodityGoodsService.getBySpuId(vo.getSpuId());
        List<PropertyValueVo> list = commodityCategoryPropertyValueService.getGoodsSkuPropertyValueListByCategoryIdAndType(goodsEntity.getCategoryId());
        return R.ok(list);
    }


}
